package com.taotao.designpattern.state.simple;

import com.google.common.collect.HashBasedTable;
import com.taotao.designpattern.state.Event;
import com.taotao.designpattern.state.State;

/**
 * 简单的调度模式
 */
public class StateMachine {

    private static HashBasedTable<State/*前一个状态*/, Event/*事件*/, State/*下一个状态*/> hashBasedTable = HashBasedTable.create();

    static {
        hashBasedTable.put(State.UN_SUBMIT, Event.SUBMIT, State.LEADER_CHECK);
        hashBasedTable.put(State.LEADER_CHECK, Event.PASS, State.HR_CHECK);
        hashBasedTable.put(State.HR_CHECK, Event.PASS, State.FINISH);
        hashBasedTable.put(State.LEADER_CHECK, Event.REJECT, State.REJECT);
        hashBasedTable.put(State.HR_CHECK, Event.REJECT, State.REJECT);
    }

    public static State getNext(State state, Event event) {
        State result = hashBasedTable.get(state, event);
        if (result == null) {
            throw new IllegalArgumentException("未找到对应状态");
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getNext(State.HR_CHECK, Event.REJECT));
    }

}

